package com.lanou3g.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanou3g.bean.Clean;
import com.lanou3g.bean.Flats;
import com.lanou3g.bean.Person;
import com.lanou3g.service.CleanService;
import com.lanou3g.service.FlatsService;
import com.lanou3g.service.PersonService;
import com.lanou3g.util.Result;

@Controller
@RequestMapping("/clean")
public class CleanController {

	@Autowired
	private CleanService cleanService;
	
	@Autowired
	private FlatsService flatsService;
	
	@Autowired
	private PersonService personService;
	
	//进入清洁列表页面
	@RequestMapping("/cleanList")
	public String cleanList() {
		return "clean/cleanList";
	}
	
	//获取清洁列表
	@ResponseBody
	@RequestMapping("/getCleanList")
	public Result getCleanList(Integer page, Integer limit) {
		return cleanService.getCleans(page, limit);
	}
	
	//进入添加清洁记录页面
	@RequestMapping("/addClean")
	public String addClean(HttpSession session) {
		
		List<Flats> flats = flatsService.getFlats();
		session.setAttribute("flats", flats);
		return "clean/addClean";
	}
	
	//根据公寓id获取人员信息
	@ResponseBody
	@RequestMapping("/getPersonsByFlatsId/{id}")
	public List<Person> getPersonsByFlatsId(@PathVariable("id") Integer id,HttpSession session) {
		List<Person> persons = personService.getPersonsByFlatsId(id);
		return persons;
	}
	
	//添加清洁记录
	@ResponseBody
	@RequestMapping("/insertClean")
	public Result insertClean(Clean clean) {
		return cleanService.insertClean(clean);
	}
	
	//进入编辑清洁记录页面
	@RequestMapping("/editClean/{id}")
	public String editClean(@PathVariable("id")Integer id,HttpSession session) {
		Clean clean = cleanService.getCleanById(id);
		session.setAttribute("clean", clean);
		//获取所有的公寓
		List<Flats> flats = flatsService.getFlats();
		session.setAttribute("flats", flats);
		//获取公寓下所有的人员
		Integer flatsId = clean.getFlatsId();
		List<Person> persons = personService.getPersonsByFlatsId(flatsId);
		session.setAttribute("persons", persons);
		return "clean/editClean";
	}
	
	//更新清洁记录
	@ResponseBody
	@RequestMapping("/updateClean")
	public Result updateClean(Clean clean) {
		return cleanService.updateClean(clean);
	}
	
	//删除指定id的清洁记录
	@ResponseBody
	@RequestMapping("/deleteClean/{id}")
	public Result deleteCleanById(@PathVariable("id")Integer id) {
		return cleanService.deleteCleanById(id);
	}
	
}
